package edu.java.texbooks.scjp.collections.test04.searching;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test {
	static String[] stringArray = { "abc", "ijk", "fgh", "bcd", "def" };

	public static void main(String[] args) {

		// test String Arrays --> use natural order(Comparable interface)
		// test01();

		// test String Arrays --> use user-defined order(Comparator interface)
		// test02();

		// test int arrays
		// test03();

		// test Collections --> use natural order(Comparable interface)
		// test04();

		// test Collections --> use user-defined order(Comparator interface)
		test05();

	}

	public static void test01() {

		// print array
		System.out.println("\nPrint array:");
		System.out.println(Arrays.toString(stringArray));

		// --------------- natural sort (Comparable) ---------------------------

		// natural sort --> use compareTo() implementation of Comparable
		// interface by String Class

		// before searching we MUST do SORT
		Arrays.sort(stringArray);
		System.out.println("\nPrint array after natural sort:");
		System.out.println(Arrays.toString(stringArray));

		// search "def" string. We have sorted the array by natural sort
		// (i.e. without Comparator object).
		// So we do searching as call binarySearch() method without Comparator
		// argument

		// binarySearch() method return int (index - non-negative or negative)
		// as result
		int index = Integer.MIN_VALUE;
		String otherString = null;

		index = Arrays.binarySearch(stringArray, "def");
		System.out.println("\n\"def\".index= " + index);

		otherString = "daf";
		index = Arrays.binarySearch(stringArray, otherString);
		System.out.println("\n\"" + otherString + "\".index= " + index);

		otherString = "zaza";
		index = Arrays.binarySearch(stringArray, otherString);
		System.out.println("\"" + otherString + "\".index= " + index);

		otherString = "aaa";
		index = Arrays.binarySearch(stringArray, otherString);
		System.out.println("\"" + otherString + "\".index= " + index);

		otherString = "abc";
		index = Arrays.binarySearch(stringArray, otherString);
		System.out.println("\"" + otherString + "\".index= " + index);
	}

	public static void test02() {
		int index = Integer.MIN_VALUE;
		String otherString = null;

		System.out.println(Arrays.toString(stringArray));

		// reversed natural order
		StringComparatorConverter scc = new StringComparatorConverter();

		// sort before searching --> use specified order
		Arrays.sort(stringArray, scc);
		System.out.println("\n\n" + Arrays.toString(stringArray));

		// searching
		System.out.println("\n\n-------------------------------\n");
		otherString = "abc";
		index = Arrays.binarySearch(stringArray, otherString, scc);
		System.out.println("\"" + otherString + "\".index= " + index);

		otherString = "fgh";
		index = Arrays.binarySearch(stringArray, otherString, scc);
		// error if we don't specify COmparator object --> unpredictable result
		// index=Arrays.binarySearch(stringArray, otherString);
		System.out.println("\"" + otherString + "\".index= " + index);

		otherString = "ijk";
		index = Arrays.binarySearch(stringArray, otherString, scc);
		System.out.println("\"" + otherString + "\".index= " + index);

		otherString = "efg";
		index = Arrays.binarySearch(stringArray, otherString, scc);
		System.out.println("\"" + otherString + "\".index= " + index);

	}

	public static void test03() {
		int[] intArray = { 10, 6, 2, 8, 2, 1 };

		System.out.println(Arrays.toString(intArray));
		int index = Integer.MIN_VALUE;

		Arrays.sort(intArray);
		System.out.println();
		System.out.println(Arrays.toString(intArray));
		index = Arrays.binarySearch(intArray, 1);
		System.out.println("\n");
		System.out.println(1 + " --> index " + index);
		System.out.println(11 + " --> index "
				+ Arrays.binarySearch(intArray, 11));

		System.out
				.println(2 + " --> index " + Arrays.binarySearch(intArray, 2));

	}

	public static void test04() {
		List<String> stringArrayList = new ArrayList<String>();
		stringArrayList.addAll(Arrays.asList(stringArray));

		System.out.println(stringArrayList);

		// natural sort
		Collections.sort(stringArrayList);
		System.out.println("\n\n" + stringArrayList);

		// searching
		int index = Integer.MAX_VALUE;
		String searchString = null;
		System.out.println("\n\n");

		searchString = "def";
		index = Collections.binarySearch(stringArrayList, searchString);
		System.out.println("\"" + searchString + "\".index= " + index);

		searchString = "efg";
		index = Collections.binarySearch(stringArrayList, searchString);
		System.out.println("\"" + searchString + "\".index= " + index);

	}

	public static void test05() {
		List<String> stringArrayList = new ArrayList<String>();
		stringArrayList.addAll(Arrays.asList(stringArray));

		System.out.println(stringArrayList);

		// user-defined order ( reversed order)
		StringComparatorConverter scc = new StringComparatorConverter();
		Collections.sort(stringArrayList, scc);

		System.out.println("\n\n" + stringArrayList);

		// searching
		int index = Integer.MAX_VALUE;
		String searchString = null;
		System.out.println("\n\n");

		searchString = "mamaMiq";
		index = Collections.binarySearch(stringArrayList, searchString, scc);
		System.out.println("\"" + searchString + "\".index= " + index);

		searchString = "aaa";
		index = Collections.binarySearch(stringArrayList, searchString, scc);
		System.out.println("\"" + searchString + "\".index= " + index);

		searchString = "abc";
		index = Collections.binarySearch(stringArrayList, searchString, scc);
		System.out.println("\"" + searchString + "\".index= " + index);

	}

}
